package com.mohe.zhihui.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Apply;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.model.Volunteer;
import com.mohe.zhihui.model.Volunteerprj;
import com.mohe.zhihui.service.interfaces.IApply;
import com.mohe.zhihui.service.interfaces.INotice;
import com.mohe.zhihui.service.interfaces.IVolunteer;
import com.mohe.zhihui.service.interfaces.IVolunteerprj;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class ApplyAction extends ActionSupport {

	private static final long serialVersionUID = 5040863892049861590L;
	
//	ApplicationContext ac;
	
	private IApply applyServ;
	private IVolunteer volunteerServ;
	private IVolunteerprj volunteerprjServ;
	
	private List<Apply> applyList;
	private Apply apply = new Apply();
	private Page page = new Page();

	public IApply getApplyServ() {
		return applyServ;
	}

	public void setApplyServ(IApply applyServ) {
		this.applyServ = applyServ;
	}

	public IVolunteer getVolunteerServ() {
		return volunteerServ;
	}

	public void setVolunteerServ(IVolunteer volunteerServ) {
		this.volunteerServ = volunteerServ;
	}

	public IVolunteerprj getVolunteerprjServ() {
		return volunteerprjServ;
	}

	public void setVolunteerprjServ(IVolunteerprj volunteerprjServ) {
		this.volunteerprjServ = volunteerprjServ;
	}

	public List<Apply> getApplyList() {
		return applyList;
	}

	public void setApplyList(List<Apply> applyList) {
		this.applyList = applyList;
	}

	public Apply getApply() {
		return apply;
	}

	public void setApply(Apply apply) {
		this.apply = apply;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
//	public ApplyAction() {
//		super();
//		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		applyServ = (IApply) ac.getBean("IApply");
//		volunteerServ = (IVolunteer) ac.getBean("IVolunteer");
//		volunteerprjServ = (IVolunteerprj) ac.getBean("IVolunteerprj");
//	}

	public String getApplyListForPage() {
		int count = applyServ.getCount(apply);
		page.setCount(count);
		applyList = applyServ.getApplyList(apply, page);
		return "applyList.jsp";
	}

	public void searchApplyList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = applyServ.getCount(apply);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Apply> list = applyServ.getApplyList(apply, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Apply s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if (!StringUtils.isBlank(s.getPhone())) {
					map.put("phone", s.getPhone());
				} else {
					map.put("phone", "");
				}
				if(s.getProject() != null ){
					map.put("project", s.getProject().getTitle());
				}
				if(s.getStatus() != null ){
					if(s.getStatus().equals(1)){
						map.put("statusStr", "审核中");
					}else if(s.getStatus().equals(2)){
						map.put("statusStr", "通过");
					}else{
						map.put("statusStr", "不通过");
					}	
				}
				map.put("status", s.getStatus() + "");
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	/**
	 * 保存申请人信息
	 */
	public void saveApply() {
		Json json = new Json();
		boolean result = applyServ.saveApply(apply);
		if (result) {
			json.setMessage("添加成功");
		} else {
			json.setSuccess(false);
			json.setMessage("添加失败");
		}
		JsonUtil.writeJson(json);
	}

	public String toEditApply() {
		apply = applyServ.getApplyById(apply.getTimeKey());
		return "updateApply.jsp";
	}

	public void updateApply() {
		Json json = new Json();
		Apply tmp = applyServ.getApplyById(apply.getTimeKey());
		if (tmp != null) {
			tmp.setName(apply.getName());
			tmp.setPhone(apply.getPhone());
			tmp.setCreateDate(new Date());
			boolean result = applyServ.updateApply(tmp);
			if (result) {
				json.setMessage("编辑成功");
				apply = tmp;
			} else {
				json.setSuccess(true);
				json.setMessage("编辑失败");
			}
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}

	public void delApply() {
		Json json = new Json();
		boolean result = applyServ.delApply(apply);
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(false);
			json.setSuccess(false);
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 确认通过
	 * @return
	 */
	public String confirmPass() {
		Apply tmp = applyServ.getApplyById(apply.getTimeKey());
		if (tmp != null) {
			// 判断该志愿者是否已经存在（以是否存在该手机号为判断标准）
			Volunteer volunteer = volunteerServ.getVolunteerByPhone(tmp.getPhone());
			Long volunteerKey;
			if(null == volunteer){// 志愿者不存在
				// 添加志愿者
				volunteer = new Volunteer();
				volunteer.setName(tmp.getName());
				volunteer.setPhone(tmp.getPhone());
				boolean save1 = volunteerServ.saveVolunteer(volunteer);
				if(save1){
					volunteer = volunteerServ.getVolunteerByPhone(tmp.getPhone());
					if(null == volunteer){
						return getApplyListForPage();
					}
					volunteerKey = volunteer.getTimeKey();
				}else{ // 志愿者添加失败
					return getApplyListForPage();
				}
			}
			// 添加Volunteerprj
			Volunteerprj volunteerprj = new Volunteerprj();
			volunteerKey = volunteer.getTimeKey();
			Long ProjectKey = tmp.getProject().getTimeKey();
			volunteerprj.setVolunteerKey(volunteerKey);
			volunteerprj.setProjectKey(ProjectKey);
			boolean save2 = volunteerprjServ.saveVolunteerprj(volunteerprj);
			if(save2){ // 志愿者-项目添加成功
				tmp.setStatus(2);
				tmp.setCreateDate(new Date());
				boolean result = applyServ.updateApply(tmp);
				if(result){ // 申请人状态更新成功
					return getApplyListForPage();
				}	
			}else{ // 志愿者-项目添加失败
				return getApplyListForPage();
			}
		} 
		
		return getApplyListForPage();
	}

}